#ifndef AJ_TCPSESSION_H
#define AJ_TCPSESSION_H

#include "aj_namespace.h"
#include <QTcpSocket>
#include <functional>
#include <QHostAddress>
#include "aj_tcpthread.h"

NAMESPACE_NETAPI_BEGIN
class TcpSession : public QTcpSocket
{
    Q_OBJECT
public:
    TcpSession(TcpThread *thread);
    ~TcpSession();

    void ConnectToServer(const QString &host,quint16 port);

    //断开连接
    void Disconnect();
    void Write(const char *data, qint64 len);

public:
    //断开连接回调
    std::function<void(void*)> OnDisConnected = nullptr;
    //读数据回调
    std::function<void(const QByteArray&)> OnRead = nullptr;

    public slots:
    //连接Server
    void SlotDoConnectToServer(const QString &host, quint16 port);
    //开始读数据
    void SlotStartRead();
    //断开连接回调
    void SlotDisConnected();
    //写数据
    void SlotDoWrite();
    //断开连接
    void SlotDoDisconnect();
signals :
    void SignalRead(const QByteArray &, int);
    void SignalDisConnected(void *);
    void SignalDoWrite();
    void SignalDoDisConnect();
    void SignalDoConnectToServer(const QString &, quint16);

private:
    TcpThread *m_thread = nullptr;
    QByteArray m_buffer = nullptr;
    QByteArray m_writeBuffer_ = nullptr;
};

NAMESPACE_NETAPI_END

#endif // AJ_TCPSESSION_H
